package cn.wolfcode.web.controller;

import cn.wolfcode.domain.*;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import javafx.fxml.LoadException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //部门页面的增删改查

    //查
    @RequirePermission(name = "员工页面",expression = "employee:list")
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //除此之外还要拿部门的部门名称,用于过滤查询回显部门名称
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        //返回
        return "employee/list";
    }

    //删
    @RequirePermission(name = "员工删除",expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.delete(id);
        //重定向会原来的list页面
        return "redirect:/employee/list";
    }

    //批量删除
    @RequirePermission(name = "员工批量删除",expression = "employee:deleteList")
    @RequestMapping("/deleteList")
    public String deleteList(Long[] ids){
        System.out.println("测试测试" + ids);
        employeeService.deleteList(ids);
        //重定向会原来的list页面
        return "redirect:/employee/list";
    }

    //增改
    @RequirePermission(name = "员工新增/编辑",expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids){
        employeeService.saveOrUpdate(employee,ids);
        return new JsonResult();
    }

    //跳转input界面
    @RequirePermission(name = "员工新增/编辑",expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        //用于显示有哪些部门
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);

        //用于显示有哪些角色
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);

        if (id != null) {
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee",employee);
        }
        //让浏览器去访问input页面,并把model的数据回显
        return "employee/input";
    }

    //ajax验证用户名是否存在
    //这个表单验证的插件,默认用valid表示true|false,使用map的方法返回回去,即插件规定的格式
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkUsername(String username) {
        Map<String,Boolean> map = new HashMap<>();
        //判断是否存在,不存在返回true
        boolean result = employeeService.checkUsername(username);
        map.put("valid",result);
        return map;
    }

    //导出
    @RequirePermission(name = "员工导出",expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(@ModelAttribute("qo") EmployeeQueryObject qo,HttpServletResponse response) throws IOException {
        //文件下载响应头,用于给文件指定文件名
        response.setHeader("Content-Disposition","attachment;filename=employee.xlsx");
        //创建POI的workbook对象
        Workbook wb = employeeService.exportXls(qo);
        //输出
        wb.write(response.getOutputStream());

    }

    //导入
    @RequirePermission(name = "员工导入",expression = "employee:importXls")
    @RequestMapping("/importXls")
    @ResponseBody
    public ImportResult importXls(MultipartFile importFile) throws IOException {
        return employeeService.importXls(importFile);
    }

    //禁用和恢复
    @RequirePermission(name = "员工禁用/恢复",expression = "employee:changeStatus")
    @RequestMapping("/status")
    @ResponseBody
    public JsonResult changeStatus(Long id,boolean status){
        //注意需要取反
        employeeService.changeStatus(id,!status);
        return new JsonResult();
    }
}
